﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.VehicleManage
{
    public partial class FaultSymptom : Common.PageBase
    {
        private UserLoginInfo _userLoginInfo;
        public string FullSymptomName
        {
            get { return (ViewState["FullSymptomName"] == null) ? "" : ViewState["FullSymptomName"].ToString(); }
            set { ViewState["FullSymptomName"] = value; }
        }

        T_FAULTSYMPTOM_BLL faultSymptomBll = new T_FAULTSYMPTOM_BLL();


        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            UserLoginInfo.CheckLoginInfo();
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);

        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                tvFaultSymptomBinding();
                gvDataBinding(_userLoginInfo.UserEntity.BUSINESSDIVISIONCODE);
            }		
        }

       

        #region 页面初始化数据绑定 
        private void tvFaultSymptomBinding()
        {
            tvFaultSymptom.Nodes.Clear();

            TreeNode pNode = new TreeNode();
            pNode.Text = "故障原因总成分类";
            pNode.Value = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            pNode.ToolTip = "-1";

            List<T_FAULTSYMPTOM_Entity> faultSymptomList = faultSymptomBll.GetList();
            string parentId = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;

            FaultSymptomRecursiveBinding(faultSymptomList, parentId, pNode);

            tvFaultSymptom.Nodes.Add(pNode);
            //tvFaultSymptom.ExpandAll();

        }

        private void FaultSymptomRecursiveBinding(List<T_FAULTSYMPTOM_Entity> faultSymptomList, string parentId, TreeNode pNode)
        {
            var subList = faultSymptomList.FindAll(delegate(T_FAULTSYMPTOM_Entity p)
                                                       {
                                                           if (p.PARENTCODE.ToLower() == parentId && p.TYPE <= 6)
                                                               return true;
                                                           else
                                                               return false;
                                                       });
            int count = faultSymptomList.Count(p =>
                                                   {
                                                       if (p.PARENTCODE.ToLower() == parentId)
                                                           return true;
                                                       else
                                                           return false;
                                                   });
            pNode.Text = pNode.Text + "(" + count + ")";
            foreach (var entity in subList)
            {
                TreeNode node = new TreeNode(entity.NAMECN, entity.CODE);
                FaultSymptomRecursiveBinding(faultSymptomList, entity.CODE, node);
                pNode.ChildNodes.Add(node);
                pNode.CollapseAll();
            }
        }

        #endregion

        


        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding(string parentId)
        {
            #region 查询条件字符串
            StringBuilder sql = new StringBuilder();
            sql.Append(" and PARENTCODE = '" + parentId + "' ");
            #endregion

            List<T_FAULTSYMPTOM_Entity> list;

            StringBuilder sb = new StringBuilder();
            faultSymptomBll.GetParentNameRecursive(faultSymptomBll.GetCache_T_FAULTSYMPTOM_List(this.Page), parentId, sb);
            FullSymptomName = sb.ToString();

            //获取总的记录条数
            gvList.RecordCount = (int)faultSymptomBll.GetCount(sql.ToString());

            list = faultSymptomBll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "AUTOID", "TYPE" };
            gvList.DataBind();

           

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            if(StringHelper.ToInt(gvList.DataKeys[e.Row.RowIndex].Values["TYPE"].ToString(), 7) <= 6)
                                            {
                                                e.Row.Cells[i].Controls.Clear();
                                            }
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion


        /// <summary>
        /// Selecteds the command.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <returns></returns>
        public string SelectedCommand(string code)
        {
            string fun = "return setValue('{0}','{1}','{2}','{3}','{4}'); ";
            List<T_FAULTSYMPTOM_Entity> list = faultSymptomBll.GetCache_T_FAULTSYMPTOM_List(this.Page);
            var entity = list.Find(p =>
                                       {
                                           if (p.CODE.ToLower() == code.ToLower())
                                               return true;
                                           else
                                               return false;
                                       });
            var pEntity = list.Find(p =>
                                        {
                                            if (p.CODE.ToLower() == entity.PARENTCODE.ToLower())
                                                return true;
                                            else
                                                return false;
                                        });

            fun = String.Format(fun, (pEntity == null) ? "" : pEntity.NAMECN, (pEntity == null) ? "" : pEntity.CODE, (entity == null) ? "" : entity.NAMECN, (entity == null) ? "" : entity.CODE, FullSymptomName + ((entity == null) ? "" : entity.NAMECN));
            return fun;
        }

        
        /// <summary>
        /// Handles the SelectedNodeChanged event of the tvFaultSymptom control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void tvFaultSymptom_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeView tv = sender as TreeView;
            gvDataBinding(tv.SelectedNode.Value);
           
        }
    }
}
